﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> //目录名获取配合DIR
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> //文件控制操作
#include <sys/types.h> //获取pid等
#include <sys/wait.h> //等待子进程结束
#include <time.h> //时间获取
#include <sys/ipc.h> //进程通信
#include <sys/msg.h> //消息队列
#include <sys/shm.h> //共享内存
#include <signal.h> //信号处理
#include <pthread.h> //线程
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "cJSON.h"
int flag = 0;
struct msg {
    char operator[10];
    int number1;
    int number2;
	int result;
};
char* fun(struct msg *k) {
	cJSON* json = NULL;
	json = cJSON_CreateObject();
	cJSON_AddNumberToObject(json, "result", k->result);
	char* dest = cJSON_Print(json);
	printf("dest:%s\n", dest);
	return dest;//记得返回dest！！！
}
void fun2(int sig) {
	flag = 1;
	exit(0);
}
int main(int argc, char *argv[])
{
	signal(SIGINT,fun2);
	char dest[1024] = {0};
	//创建套接字
	int sock = 0;
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1) {
		perror("socket");
	}

	int ret = 0;

	//绑定地址
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(5497);
	addr.sin_addr.s_addr = inet_addr("192.168.48.128");
	bind(sock, (struct sockaddr*)&addr, sizeof(addr));

	//接收客户端的信息
	struct sockaddr_in client;
	int len = sizeof(client);
	while (flag == 0) {
		ret = recvfrom(sock, dest, sizeof(dest), 0, (struct sockaddr*)&client, &len);
		if (ret == -1) {
			perror("recvfrom");
		}
		printf("recvfrom:%s\n", dest);

		//解析json
		struct msg k;

		cJSON* json = NULL;
		json = cJSON_Parse(dest);
		if (json == NULL) {
			printf("解析error\n");
		}

		cJSON* num1 = NULL;
		num1 = cJSON_GetObjectItem(json, "number1");
		if (num1 == NULL) {
			printf("读取error\n");
		}
		if (num1->type == cJSON_Number) {
			k.number1 = num1->valueint;
		}

		cJSON* num2 = NULL;
		num2 = cJSON_GetObjectItem(json, "number2");
		if (num2 == NULL) {
			printf("读取error\n");
		}
		if (num2->type == cJSON_Number) {
			k.number2 = num2->valueint;
		}
		cJSON* op = NULL;
		op = cJSON_GetObjectItem(json, "operator");
		if (op == NULL) {
			printf("读取error\n");
		}
		if (op->type == cJSON_String) {
			strcpy(k.operator, op->valuestring);
			if (strcmp(k.operator, "add") == 0) {
				k.result = k.number1 + k.number2;
				char* dest = fun(&k);//记得接收返回值！！！
				ret = sendto(sock, dest, strlen(dest), 0, (struct sockaddr*)&client, len);
			}
			else if (strcmp(k.operator, "sub") == 0) {
				k.result = k.number1 - k.number2;
				char* dest = fun(&k);
				ret = sendto(sock, dest, strlen(dest), 0, (struct sockaddr*)&client, len);
			}
			else if (strcmp(k.operator, "mul") == 0) {
				k.result = k.number1 * k.number2;
				char* dest = fun(&k);
				ret = sendto(sock, dest, strlen(dest), 0, (struct sockaddr*)&client, len);
			}
			else if (strcmp(k.operator, "div") == 0) {
				k.result = k.number1 / k.number2;
				char* dest = fun(&k);
				ret = sendto(sock, dest, strlen(dest), 0, (struct sockaddr*)&client, len);
			}
		}
	}
	close(sock);
	return 0;
}